package mianyang.zhangsan.com.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.pojo.Leave;
import mianyang.zhangsan.com.service.LeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/leave")
public class LeaveController {
    @Autowired
    private LeaveService leaveService;

    /**
     *请假列表请求
     * @param page
     * @param rows
     * @param studentId
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestParam(value = "page", defaultValue = "1")int page,
                       @RequestParam(value = "rows", defaultValue = "10")int rows,
                       @RequestParam(value = "studentId", defaultValue = "0")int studentId){
        Page<Leave> leavePage = leaveService.queryByPage(page, rows, studentId);
        Map<String, Object> map = new HashMap<>();
        map.put("total", leavePage.getPages());
        map.put("rows", leavePage.getRecords());
        return map;
    }

    /**
     * 添加请假信息
     * @param leave
     * @return
     */
    @RequestMapping(value = "/add", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addLeave(Leave leave){
        if (leave != null){
            leaveService.addLeave(leave);
            return "{\"success\": true, \"message\": \"添加成功\"}";
        }
        return "{\"success\": false}";
    }

    /**
     * 删除请假信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String deleteLeave(int id){
        if (id != 0){
            leaveService.deleteLeave(id);
            return "{\"success\": true, \"message\": \"删除成功\"}";
        }
        return "{\"success\": false}";
    }

    /**
     * 审核请假信息
     * @param leave
     * @return
     */
    @RequestMapping(value = "/check", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String checkLeave(Leave leave){
        if (leave != null){
            System.out.println(leave);
            return "{\"success\": true, \"message\": \"审核通过\"}";
        }
        return "{\"success\": false, \"message\": \"数据包含敏感词汇\"}";
    }

    /**
     * 更新请假信息
     * @param id
     * @param studentId
     * @param info
     * @return
     */
    @RequestMapping(value = "/update", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updateLeave(int id, int studentId, String info){
        if (id != 0){
            leaveService.updateLeave(id, studentId, info);
            return "{\"success\": true, \"message\": \"更新成功\"}";
        }
        return "{\"success\": false}";
    }

}
